package wine.web;


import javax.naming.Context;
import javax.naming.InitialContext;

import wine.ejb.utilities.Log;
import wine.ejb.utilities.Utility;
import wine.logic.bean.WInformationDao;
import wine.logic.bean.WModuleDao;
import wine.logic.bean.WUsrInfDao;
import wine.logic.interfaces.WInformationImpl;
import wine.logic.interfaces.WModuleImpl;
import wine.logic.interfaces.WUsrInfImpl;

import com.opensymphony.xwork2.ActionSupport;

public class EjbSupport extends ActionSupport{
	private static final long serialVersionUID = 1L;
	private WUsrInfDao wUsrInfDao = null;
	private WModuleDao wModuleDao = null;
	private WInformationDao wInformationDao = null;
	
	protected WUsrInfDao getWUsrInfDao() {
		if(!Utility.isNotEmpty(wUsrInfDao)){
			wUsrInfDao=(WUsrInfDao) lookupDao(WUsrInfImpl.class);
		}
		return wUsrInfDao;
	}
	protected WModuleDao getWModuleDao() {
		if(!Utility.isNotEmpty(wModuleDao)){
			wModuleDao=(WModuleDao) lookupDao(WModuleImpl.class);
		}
		return wModuleDao;
	}
	protected WInformationDao getWInformationDao() {
		if(!Utility.isNotEmpty(wInformationDao)){
			wInformationDao=(WInformationDao) lookupDao(WInformationImpl.class);
		}
		return wInformationDao;
	}
	/**
	 * 获得远程接口
	 * @param implClass
	 * @return
	 */
	protected Object lookupDao(Class<?> implClass) {
		try {
			Context ctx = new InitialContext();
			Log.debug(implClass.getSimpleName());
			return ctx.lookup(implClass.getSimpleName() + "/remote");
		} catch (Exception e) {
			Log.error(e.getMessage());
			return null;
		}
	}
	/**
	 * 获得远程接口
	 * @param implClass
	 * @return
	 */
	protected Object lookupLocalDao(Class<?> implClass) {
		try {
			Context ctx = new InitialContext();
			Log.debug(implClass.getSimpleName());
			return ctx.lookup(implClass.getSimpleName() + "/local");
		} catch (Exception e) {
			Log.error(e.getMessage());
			return null;
		}
	}
	/**
	 * 得到数据连接流
	 * @param output
	 * @param jasperFile
	 * @param params
	 * @return
	 */
	/*
	protected OutputStream createReport(OutputStream output,String jasperFile, Map params){
		InputStream reportStream =getClass().getClassLoader().getResourceAsStream(Utility.getFormatPath(jasperFile));
		//InputStream reportStream = ServletActionContext.getServletContext().getResourceAsStream(jasperFile);
		
		Context ctx = null;
		Connection conn = null;
		try {
	         ctx = new InitialContext();
	         DataSource dts = (DataSource)ctx.lookup("java:mssql-realname");
	         conn = dts.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			JasperRunManager.runReportToPdfStream(reportStream,output, params, conn);
			
			conn.close();
		} catch (JRException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
			e.printStackTrace();
		}
		return output;
	}*/
	protected Object lookupDao(Object obj,Class implClass) {
		if(Utility.isNotEmpty(obj)){
			return obj;
		}
		return lookupDao(implClass);
	}

}